﻿/* 
 * author: www.cnblogs.com/luminji
 * date  : 2012/01/25
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity.InterceptionExtension;
using Luminji.Core.Enterprise.RBAC;

namespace Luminji.Core.Enterprise
{
    public sealed class ContainerFactory
    {

        public static IUnityContainer GetContainer()
        {
            var container = new UnityContainer();
            ExeConfigurationFileMap infraFileMap = new ExeConfigurationFileMap();
            //infraFileMap.ExeConfigFilename = Environment.CurrentDirectory + @"\NewFolder1\AppconfigSampe.xml";
            infraFileMap.ExeConfigFilename = @"NewFolder1\AppconfigSampe.xml";
            UnityConfigurationSection infraConfig = (UnityConfigurationSection)ConfigurationManager
                .OpenMappedExeConfiguration(infraFileMap, ConfigurationUserLevel.None)
                .GetSection("unity");
            infraConfig.Configure(container, "containerOne");
            return container;
        }

        public static IUnityContainer GetContainer(string configFilename, string sectionName, string containerName)
        {
            var container = new UnityContainer();
            container.AddNewExtension<Interception>();
            
            InitContainer(configFilename, sectionName, containerName, container);
            return container;
        }

        private static void InitContainer(string configFilename, string sectionName, string containerName, IUnityContainer container)
        {
            ExeConfigurationFileMap infraFileMap = new ExeConfigurationFileMap();
            //infraFileMap.ExeConfigFilename = Environment.CurrentDirectory + @"\NewFolder1\AppconfigSampe.xml";
            infraFileMap.ExeConfigFilename = configFilename;
            UnityConfigurationSection infraConfig = (UnityConfigurationSection)ConfigurationManager
                .OpenMappedExeConfiguration(infraFileMap, ConfigurationUserLevel.None)
                .GetSection(sectionName);
            infraConfig.Configure(container, containerName);
        }
    }
}
